<?php
class BMSC_Account extends BMSC_Db
{
	public function getAccountCount()
	{
		try
		{			
			$query = "CALL sp_admin_get_account_count()";
			$stmt = $this->_readerDb->query($query);
			$r = $stmt->fetchAll();
			return empty($r) ? 0 : $r[0];			
		} catch (Exception $ex)	{
			throw $ex;
		}
	}

	public function getAccounts($from, $numberOfAccounts, $statusSorting)
	{
		try
		{
			//file_put_contents('/home/vhosts/bmsc_admin/www/debug.txt', $statusSorting);			
			$query = "CALL sp_admin_get_account_list(?, ?, ?)";
			$stmt = $this->_readerDb->query($query, array($from, $numberOfAccounts, $statusSorting));
			$accounts = $stmt->fetchAll();
			return $accounts;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function getAccountStatus($accountID)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_account_status(%u)", $accountID);
			$account = $this->_readerDb->fetchRow($query);
			$this->_readerDb->closeConnection();
			return $account;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	// Cập nhật thông tin account vào database
	public function editAccountStatus($accountID, $accountStatus, $updateBy)
	{
		try
		{
			$accountInfo = array();
			$accountInfo["STATUS"] = $accountStatus;
			$accountInfo["UPDATE_BY"] = $updateBy;
			$accountInfo["UPDATE_DATE"] = date("Y-m-d H:i:s");
			return $this->_writerDb->update("open_account", $accountInfo, "ID = " . $accountID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function removeAccount($accountID, $updateBy)
	{
		try
		{
			$accountInfo = array();
			$accountInfo["IS_DELETE"] = 1;
			$accountInfo["UPDATE_BY"] = $updateBy;
			$accountInfo["UPDATE_DATE"] = date("Y-m-d H:i:s");
			return $this->_writerDb->update("open_account", $accountInfo, "ID=" . $accountID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
}
?>